package com.hsq.androidproject.ui.fragment;


import android.content.res.Resources;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.support.v4.view.ViewPager;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.example.lenovo.androidproject3.R;
import com.hsq.androidproject.adapter.MyFragmentPagerAdapter;
import com.hsq.androidproject.ui.SearchActivity;
import com.hsq.androidproject.ui.recommend_fragment.RRecommendFragment;
import com.hsq.androidproject.ui.recommend_fragment.RStarFragment;
import com.hsq.androidproject.ui.recommend_fragment.RTvFragment;

import java.util.ArrayList;

/**
 * A simple {@link Fragment} subclass.
 */
public class RecommendFragment extends BaseFragment {
    private ViewPager mPager;
    private TextView barText;
    private TextView tv_star, tv_recommendy, tv_tv;
    private ImageView iv_search;
    LinearLayout ll_widht;

    Resources resources;
    private int currIndex = 0;
    private ArrayList<Fragment> fragmentsList;
    RStarFragment rStarFragment;
    RRecommendFragment rRecommendFragment;
    RTvFragment rTvFragment;

    public RecommendFragment() {
        // Required empty public constructor
    }

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {
        // Inflate the layout for this fragment
        View view = inflater.inflate(R.layout.fragment_recommend, container, false);
        resources = getResources();
        ll_widht = (LinearLayout) view.findViewById(R.id.ll_widht);
        InitTextView(view);
        InitTextBar(view);
        InitViewPager(view);
        initImage(view);
        return view;
    }

    private void initImage(View view) {
        iv_search = (ImageView) view.findViewById(R.id.iv_search);
        iv_search.setOnClickListener(new View.OnClickListener() {      /////跳转到SearchActivity
            @Override
            public void onClick(View v) {
                intent2Activity(SearchActivity.class);
            }
        });
    }

    /**
     * 初始化三个textViewhe点击切换ViewPager
     */
    private void InitTextView(View parentView) {
        tv_star = (TextView) parentView.findViewById(R.id.tv_star);
        tv_recommendy = (TextView) parentView.findViewById(R.id.tv_recommendy);
        tv_tv = (TextView) parentView.findViewById(R.id.tv_tv);

        tv_star.setOnClickListener(new MyOnClickListener(0));
        tv_recommendy.setOnClickListener(new MyOnClickListener(1));
        tv_tv.setOnClickListener(new MyOnClickListener(2));
    }

    public class MyOnClickListener implements View.OnClickListener {
        private int index = 0;

        public MyOnClickListener(int i) {
            index = i;
        }

        @Override
        public void onClick(View v) {
            mPager.setCurrentItem(index);
        }
    }

    /*
     * 初始化图片的位移像素
     */
    public void InitTextBar(View view) {
        barText = (TextView) view.findViewById(R.id.cursor);

        int width = getViewWidth(ll_widht) + 240;
        int tabLineLength = width / 3;
        RelativeLayout.LayoutParams lp = (RelativeLayout.LayoutParams) barText.getLayoutParams();
        lp.width = tabLineLength;
        barText.setLayoutParams(lp);

    }

    //获得LinearLayout的宽度
    public int getViewWidth(LinearLayout view) {
        view.measure(RelativeLayout.LayoutParams.WRAP_CONTENT, RelativeLayout.LayoutParams.WRAP_CONTENT);
        return view.getMeasuredWidth();
    }

    /**
     * 初始化ViewPager
     */
    private void InitViewPager(View parentView) {
        mPager = (ViewPager) parentView.findViewById(R.id.vPager);
        fragmentsList = new ArrayList<>();

        rStarFragment = new RStarFragment();
        rRecommendFragment = new RRecommendFragment();
        rTvFragment = new RTvFragment();
        fragmentsList.add(rStarFragment);
        fragmentsList.add(rRecommendFragment);
        fragmentsList.add(rTvFragment);

        //给ViewPager设置适配器
        mPager.setAdapter(new MyFragmentPagerAdapter(getChildFragmentManager(), fragmentsList));
        mPager.setOnPageChangeListener(new MyOnPageChangeListener());//页面变化时的监听器
        mPager.setCurrentItem(1);//设置当前显示标签页为第一页
    }

    /////重新viewPager的点击事件
    public class MyOnPageChangeListener implements ViewPager.OnPageChangeListener {
        @Override
        public void onPageSelected(int arg0) {
            currIndex = arg0;
            int i = currIndex + 1;
            if (i == 1) {
                tv_star.setTextColor(resources.getColor(R.color.white));
                tv_recommendy.setTextColor(resources.getColor(R.color.bg_gray_pressed));
                tv_tv.setTextColor(resources.getColor(R.color.bg_gray_pressed));
            } else if (i == 2) {
                tv_star.setTextColor(resources.getColor(R.color.bg_gray_pressed));
                tv_recommendy.setTextColor(resources.getColor(R.color.white));
                tv_tv.setTextColor(resources.getColor(R.color.bg_gray_pressed));
            } else if (i == 3) {
                tv_star.setTextColor(resources.getColor(R.color.bg_gray_pressed));
                tv_recommendy.setTextColor(resources.getColor(R.color.bg_gray_pressed));
                tv_tv.setTextColor(resources.getColor(R.color.white));
            }
        }

        @Override
        public void onPageScrolled(int arg0, float arg1, int arg2) {
            // 取得该控件的实例
            RelativeLayout.LayoutParams ll = (android.widget.RelativeLayout.LayoutParams) barText
                    .getLayoutParams();

            if (currIndex == arg0) {
                ll.leftMargin = (int) (currIndex * barText.getWidth() + arg1
                        * barText.getWidth());
            } else if (currIndex > arg0) {
                ll.leftMargin = (int) (currIndex * barText.getWidth() - (1 - arg1) * barText.getWidth());
            }
            barText.setLayoutParams(ll);
        }

        @Override
        public void onPageScrollStateChanged(int arg0) {
        }
    }
}
